/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       discrete_event.cpp

	$Header: /game/discrete_event.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "discrete_event.h"

#include <algorithm>

#include "adventure_frame.h"
#include "adventure_map_window.h"
#include "basic_dialog.h"
#include "enum_operations.h"
#include "player.h"
#include "sized_int_types.h"
#include "streambuf_operators.h"

// --------------------------------------------------------------------------
// Unnamed namespace
// --------------------------------------------------------------------------

namespace
{

	// --------------------------------------------------------------------------
	// --------------------------------------------------------------------------
	inline int to_base_map_format_version( int format_version )
	{
		return 0;
	}

} // Unnamed namespace

// --------------------------------------------------------------------------
// t_discrete_event members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
t_discrete_event::t_discrete_event()
{
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
void t_discrete_event::execute_effects(t_player& current_player, t_adventure_frame* frame) const
{
	current_player.gain(m_material_amounts);

	if (!m_message.empty() && !current_player.is_computer()) {
		frame->get_map_window()->halt_movement();
		t_counted_ptr<t_basic_dialog> dialog_ptr = new t_basic_dialog;	
		dialog_ptr->add_materials(m_material_amounts);
		dialog_ptr->set_text( m_message, k_justify_left );
		dialog_ptr->add_ok_button();
		dialog_ptr->open();
		dialog_ptr->run_modal();
	}
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_discrete_event::read(
	std::streambuf &	stream,
	int					version )
{
	if (	!t_script_event::read( stream, version )
		||	!read_string16( stream, m_message ) )
		return false;

	t_material material;
	for (	material = t_material( 0 );
			material < k_material_count;
			enum_incr( material ) )
		m_material_amounts[ material ] = get< t_int32 >( stream );

	return true;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_discrete_event::read_from_map(
	std::streambuf &	stream,
	int					format_version )
{
	if (	!t_script_event::read_from_map( stream, to_base_map_format_version( format_version ) )
		||	!read_string16( stream, m_message ) )
		return false;

	t_material material;
	for (	material = t_material( 0 );
			material < k_material_count;
			enum_incr( material ) )
		m_material_amounts[ material ] = get< t_int32 >( stream );

	return true;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_discrete_event::write( std::streambuf & stream ) const
{
	if (	!t_script_event::write( stream )
		||	!write_string16( stream, m_message ) )
		return false;

	t_material material;
	for (	material = t_material( 0 );
			material < k_material_count;
			enum_incr( material ) )
		put< t_int32 >( stream, m_material_amounts[ material ] );

	return true;
}

